# -*- coding: utf-8 -*-
# @Time : 2022/3/22 9:50
# @FileName: UpgradeMessage.py
# @Mail: lizhigen1996@aliyun.com

__author__ = 'Zhigen.li'

from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import pyqtSignal, QUrl
from PyQt5.QtGui import QDesktopServices
from threading import Thread
from threading import RLock
import time

class UpgradeMessage(QMessageBox):
    closeMsgBoxSignal = pyqtSignal()
    upgradeMsgBoxTextSignal = pyqtSignal(str)

    def __init__(self, parent, db):
        super(UpgradeMessage, self).__init__(parent)

        self.parent = parent
        self.db = db
        self.th = Thread(target=self.closeThread)
        self.timer = 0.0
        self.lock = RLock()
        self.isMouseEnter = False

        self.closeMsgBoxSignal.connect(self.onCloseMsgBox)
        self.upgradeMsgBoxTextSignal.connect(self.onUpgradeMsgBoxText)

    def checkNotification(self):
        hasNote, currVer, newVer, msg = self.db.checkNotification()
        if hasNote:
            self.setIcon(QMessageBox.Icon.Question)
            self.setWindowTitle('提醒(5.0s后自动关闭)')
            self.setText(msg.format(currVer, newVer))
            self.addButton(QMessageBox.Yes)
            self.button(QMessageBox.Yes).setText('确认')
            self.th.start()
            self.exec()

    def checkVersion(self):
        hasNewVer, currVer, newVer = self.db.checkNewVersion()
        if hasNewVer:
            self.setIcon(QMessageBox.Icon.Question)
            self.setWindowTitle('提醒(5.0s后自动关闭)')
            self.setText('当前版本：{}，新版本：{}，是否更新？'.format(currVer, newVer))
            self.addButton(QMessageBox.Yes)
            self.addButton(QMessageBox.Cancel)
            self.button(QMessageBox.Cancel).clicked.connect(self.onClickCancelBtn)
            self.button(QMessageBox.Yes).setText('确认')
            self.button(QMessageBox.Cancel).setText('取消')
            self.th.start()
            self.exec()
            if self.clickedButton() == self.button(QMessageBox.Yes):
                QDesktopServices.openUrl(QUrl("http://www.zhigen.cool"))
            return True
        return False

    def closeThread(self):
        while self.timer < 5.0:
            time.sleep(1.0)
            self.lock.acquire()
            if not self.isMouseEnter:
                self.timer += 1.0
            self.lock.release()
            self.upgradeMsgBoxTextSignal.emit('提醒({}s后自动关闭)'.format(5.0 - self.timer))
        if self.isVisible():
            self.closeMsgBoxSignal.emit()
        self.timer = 0.0

    def onCloseMsgBox(self):
        self.close()

    def onUpgradeMsgBoxText(self, s):
        self.setWindowTitle(s)

    def enterEvent(self, a0):
        self.lock.acquire()
        self.isMouseEnter = True
        self.lock.release()

    def leaveEvent(self, a0):
        self.lock.acquire()
        self.isMouseEnter = False
        self.lock.release()

    def closeEvent(self, a0):
        self.onClickCancelBtn()

    def onClickCancelBtn(self):
        self.timer = 5.5
        if self.th.is_alive():
            self.th.join()
